package cn.iocoder.yudao.module.system.controller.admin.examclass;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.system.controller.admin.examclass.vo.*;
import cn.iocoder.yudao.module.system.convert.examclass.ExamClassConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.examclass.ExamClassDO;
import cn.iocoder.yudao.module.system.service.examclass.ExamClassService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 班级信息")
@RestController
@RequestMapping("/system/exam-class")
@Validated
public class ExamClassController {

    @Resource
    private ExamClassService examClassService;

    @PostMapping("/create")
    @Operation(summary = "创建班级信息")
    @ApiAccessLog(operateType = CREATE)
    public CommonResult<Long> createExamClass(@Valid @RequestBody ExamClassCreateReqVO createReqVO) {
        return success(examClassService.createExamClass(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新班级信息")
    @ApiAccessLog(operateType = UPDATE)
    public CommonResult<Boolean> updateExamClass(@Valid @RequestBody ExamClassUpdateReqVO updateReqVO) {
        examClassService.updateExamClass(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除班级信息")
    @Parameter(name = "id", description = "编号", required = true)
    @ApiAccessLog(operateType = DELETE)
    public CommonResult<Boolean> deleteExamClass(@RequestParam("id") Long id) {
        examClassService.deleteExamClass(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得班级信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:exam-class:query')")
    @DataPermission
    public CommonResult<ExamClassRespVO> getExamClass(@RequestParam("id") Long id) {
        ExamClassDO examClass = examClassService.getExamClass(id);
        return success(ExamClassConvert.INSTANCE.convert(examClass));
    }

    @GetMapping("/list")
    @Operation(summary = "获得班级信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<ExamClassRespVO>> getExamClassList(@RequestParam("ids") Collection<Long> ids) {
        List<ExamClassDO> list = examClassService.getExamClassList(ids);
        return success(ExamClassConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得班级信息分页")
    @PreAuthorize("@ss.hasPermission('system:exam-class:query')")
    @DataPermission
    public CommonResult<PageResult<ExamClassRespVO>> getExamClassPage(@Valid ExamClassPageReqVO pageReqVO) {
        PageResult<ExamClassDO> pageResult = examClassService.getExamClassPage(pageReqVO);
        return success(ExamClassConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出班级信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:exam-class:export')")
    @DataPermission
    @ApiAccessLog(operateType = EXPORT)
    public void exportExamClassExcel(@Valid ExamClassPageReqVO exportReqVO,
                                     HttpServletResponse response) throws IOException {
        List<ExamClassDO> list = examClassService.getExamClassList(exportReqVO);
        // 导出 Excel
        List<ExamClassRespVO> datas = ExamClassConvert.INSTANCE.convertList(list);
        ExcelUtils.write(response, "班级信息.xls", "数据", ExamClassRespVO.class, datas);
    }

    @GetMapping("/list-by-teacher")
    @Operation(summary = "根据教师ID获取班级列表")
    @Parameter(name = "teacherId", description = "教师ID", required = true, example = "1")
    public CommonResult<List<ExamClassRespVO>> getExamClassListByTeacher(@RequestParam("teacherId") Long teacherId) {
        List<ExamClassDO> list = examClassService.getExamClassListByTeacherId(teacherId);
        return success(ExamClassConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list-by-grade")
    @Operation(summary = "根据年级获取班级列表")
    @Parameter(name = "grade", description = "年级", required = true, example = "高三")
    public CommonResult<List<ExamClassRespVO>> getExamClassListByGrade(@RequestParam("grade") String grade) {
        List<ExamClassDO> list = examClassService.getExamClassListByGrade(grade);
        return success(ExamClassConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/export-teacher-accounts")
    @Operation(summary = "导出班主任账号信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:exam-class:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTeacherAccountsExcel(@Valid ExamClassPageReqVO exportReqVO,
                                           HttpServletResponse response) throws IOException {
        List<TeacherAccountExportExcelVO> list = examClassService.getTeacherAccountsForExport(exportReqVO);
        
        // 生成包含筛选条件的文件名
        String fileName = "班主任账号信息.xls";
        if (exportReqVO.getGrade() != null && !exportReqVO.getGrade().trim().isEmpty()) {
            // 如果指定了年级，文件名包含年级信息
            fileName = String.format("%s-班主任账号信息.xls", exportReqVO.getGrade());
        } else if (exportReqVO.getClassName() != null && !exportReqVO.getClassName().trim().isEmpty()) {
            // 如果指定了班级名称，文件名包含班级信息
            fileName = String.format("%s班级-班主任账号信息.xls", exportReqVO.getClassName());
        } else {
            // 默认包含导出时间
            fileName = String.format("全部班主任账号信息-%s.xls", 
                java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyyMMdd-HHmm")));
        }
        
        // 导出 Excel
        ExcelUtils.write(response, fileName, "数据", TeacherAccountExportExcelVO.class, list);
    }

}